package nuist.hanniko.stu.webbackend.dao.mapper;

import nuist.hanniko.stu.webbackend.model.DeviceGroup;
import nuist.hanniko.stu.webbackend.model.GroupMemberDevice;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public interface DeviceGroupMapper {

    @Insert("insert into device_group(device_group_name, creator, create_time)" +
            "values (#{groupId}, #{creator}, #{createTime})")
    int addGroup(DeviceGroup group);

    @Insert("insert into device_to_group_map(device_group_name, device_id, add_time)" +
            "values (#{groupName}, #{deviceId}, #{addTime})")
    int addDevice(GroupMemberDevice device);

    @Select("select * from device_to_group_map where device_group_name=#{groupId})")
    @Results({
            @Result(property = "groupName", column = "device_group_name"),
            @Result(property = "deviceId", column = "device_id"),
            @Result(property = "addTime", column = "add_time")
    })
    List<GroupMemberDevice> getAllByGroupId(String groupId);

    @Select("select * from device_group where creator=#{creator}")
    @Results({
            @Result(property = "groupId", column = "device_group_name"),
            @Result(property = "creator", column = "creator"),
            @Result(property = "createTime", column = "create_time")
    })
    List<DeviceGroup> getAllGroupByOwner(String creator);
}
